package cn.xz.xy.activity


import android.os.Handler
import android.util.Log
import android.widget.SeekBar
import cn.xz.xy.R
import cn.xz.xy.base.BaseActivity
import cn.xz.xy.bean.CommonLib
import cn.xz.xy.bean.DeviceList
import cn.xz.xy.bean.U3dListBean
import cn.xz.xy.databinding.ActivitySecondBinding
import cn.xz.xy.databinding.ActivityThirdBinding
import cn.xz.xy.net.ControlDevice
import cn.xz.xy.net.HttpEvent
import cn.xz.xy.util.*

class ThirdActivity : BaseActivity<ActivityThirdBinding>(R.layout.activity_third) {

    //设备信息
    private var mDevice: DeviceList? = null

    //当前选择的主机
    private var mTag = 0


    //当前视频进度值
    private var pMove = 0
    private var handler: Handler? = null

    private val us3List = intArrayOf(14034,14035,14033,14046,14050,14051,14118)

    override fun initData() {
        //获取跳转数据
        mTag = intent.getIntExtra(CommonLib.JUMP_TYPE, -1)
        mDevice = ControlDevice.getThisDevice(mTag)
        //mDevice?.ip = "192.168.10.203"

        if (mDevice == null){
            ToastUtil.show("数据异常")
            finish()
        }else{
            //设置标题
            mDataBind.tvTitle.text = mDevice!!.name
            //获取当前音量
            getVoice()
            //开启定时器获取视频进度
            handler = Handler()
            handler?.post(movieR)
        }
    }

    override fun initEvent() {
        //返回
        mDataBind.tvBack.setOnClickListener {
            finish()
        }
        //播放
        mDataBind.zkPlay.setOnClickListener {
            if (is3D()){
                U3dHttp.PlayMovie(mDevice!!.ip)
            }else{
                LocalHttp.moviePlay(mDevice!!.ip,1)
            }
        }
        //暂停
        mDataBind.zkPause.setOnClickListener {
            if (is3D()){
                U3dHttp.PauseMovie(mDevice!!.ip)
            }else{
                LocalHttp.moviePlay(mDevice!!.ip,2)
            }
        }
        //重播
        mDataBind.zkReplay.setOnClickListener {
            if (is3D()){
                U3dHttp.ReplayMovie(mDevice!!.ip)
            }else{
                LocalHttp.moviePlay(mDevice!!.ip,3)
            }
        }
        //上一页
        mDataBind.zkPageUp.setOnClickListener {
            if (is3D()){
                U3dHttp.PrevPage(mDevice!!.ip)
            }else{
                LocalHttp.moviePlay(mDevice!!.ip,9)
            }
        }
        //下一页
        mDataBind.zkPageDown.setOnClickListener {
            if (is3D()){
                U3dHttp.NextPage(mDevice!!.ip)
            }else{
                LocalHttp.moviePlay(mDevice!!.ip,10)
            }
        }
        //声音滑动监听
        mDataBind.zkVolumeSeekbar.setOnSeekBarChangeListener(object :
            SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
                if (mDevice != null){
                    SeekBarPopUtils.move(progress, seekBar, false)
                    LocalHttp.changeVoice(mDevice!!.ip,progress)
                }
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {
                if (mDevice != null){
                    SeekBarPopUtils.showPop(seekBar)
                }
            }

            override fun onStopTrackingTouch(seekBar: SeekBar) {
                if (mDevice != null){
                    SeekBarPopUtils.dismiss()
                }
            }
        })
        //视频滑动监听
        mDataBind.zkVideoSeekbar.setOnSeekBarChangeListener(object :
            SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
                if (mDevice != null){
                    SeekBarPopUtils.move(progress, seekBar, true)
                    pMove = progress
                }
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {
                if (mDevice != null){
                    SeekBarPopUtils.showPop(seekBar)
                }
            }

            override fun onStopTrackingTouch(seekBar: SeekBar) {
                if (mDevice != null){
                    SeekBarPopUtils.dismiss()
                    if (is3D()){
                        U3dHttp.changeMovie(mDevice!!.ip,pMove)
                    }else{
                        LocalHttp.changeMovie(mDevice!!.ip,pMove)
                    }
                }
            }
        })

    }

    //判断是否是3d
    private fun is3D(): Boolean{
        var get = false
        us3List.forEach {
            if (it == mTag){
                get = true;
            }
        }
        return get
    }


    private fun getVoice(){
        LocalHttp.getVoiceSize(mDevice!!.ip,LocalHttp.GetVoiceSizeListener {
            mDataBind.zkVolumeSeekbar.post {
                mDataBind.zkVolumeSeekbar.progress = it
            }
        })
    }

    private var movieR: Runnable = object : Runnable {
        override fun run() {
            getMove()
            handler?.postDelayed(this, 1000)
        }
    }

    private fun getMove(){
        if (is3D()){
            //获取程序的
            U3dHttp.getMovieSize(mDevice!!.ip,U3dHttp.GetMovieSizeListener { maxTxt, minTxt, maxSeekBar, seekProgress ->
                mDataBind.zkVideoSeekbar.post {
                    mDataBind.zkMaxTime.text = maxTxt
                    mDataBind.zkMinTime.text = minTxt
                    mDataBind.zkVideoSeekbar.max = maxSeekBar
                    mDataBind.zkVideoSeekbar.progress = seekProgress
                }
            })
        }else{
            //客户端的
            LocalHttp.getMovieSize(mDevice!!.ip,LocalHttp.GetMovieSizeListener { maxTxt, minTxt, maxSeekBar, seekProgress ->
                mDataBind.zkVideoSeekbar.post {
                    mDataBind.zkMaxTime.text = maxTxt
                    mDataBind.zkMinTime.text = minTxt
                    mDataBind.zkVideoSeekbar.max = maxSeekBar
                    mDataBind.zkVideoSeekbar.progress = seekProgress
                }
            })
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        //及时置空
        if (handler != null){
            handler?.removeCallbacks(movieR)
            handler = null
        }
    }

}